package com.example.afinal.Audio;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.example.afinal.R;

import java.io.File;

public class PlayRecordingActivity extends AppCompatActivity {
    private MediaPlayer mediaPlayer = new MediaPlayer();
    private ImageView ivPlayButton;
    private ImageView ivPlay;
    private File file;
    private TextView tvName;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_play_recording);
        ivPlay = (ImageView) findViewById(R.id.iv_play);
        tvName = (TextView) findViewById(R.id.tv_name);

        Intent intent = getIntent();
        Bundle bundle = intent.getBundleExtra("data");
        file = (File) bundle.getSerializable("file");
        tvName.setText(file.getName());
        ivPlayButton = (ImageView) findViewById(R.id.iv_play_button);
        ivPlayButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!mediaPlayer.isPlaying()) {
                    mediaPlayer.start();
                    ivPlay.setImageDrawable(getResources().getDrawable(R.drawable.zanting));
                } else {
                    mediaPlayer.reset();
                    initMediaPlayer();//初始化播放器 MediaPlayer
                    ivPlay.setImageDrawable(getResources().getDrawable(R.drawable.bofang));
                }
            }
        });

        mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mediaPlayer) {
                // todo
                ivPlay.setImageDrawable(getResources().getDrawable(R.drawable.bofang));
            }
        });
        //权限判断，如果没有权限就请求权限
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
        } else {
            initMediaPlayer();//初始化播放器 MediaPlayer
        }
    }

    private void initMediaPlayer() {
        try {
            //File files = new File(Environment.getExternalStorageDirectory(), "music.mp3");
            mediaPlayer.setDataSource(file.getPath());//指定音频文件路径
            //Log.i("file",file.getPath());
            mediaPlayer.setLooping(false);//设置为循环播放
            mediaPlayer.prepare();//初始化播放器MediaPlayer

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case 1:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    initMediaPlayer();
                } else {
                    Toast.makeText(this, "拒绝权限，将无法使用程序。", Toast.LENGTH_LONG).show();
                    finish();
                }
                break;
            default:
        }

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mediaPlayer != null) {
            mediaPlayer.stop();
            mediaPlayer.release();
        }
    }
}
